﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="Replacement.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   Defines the Replacement type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace ScrumForTeamSystem.EventService.TransitionService.DataStructures
{
    using System.Xml.Serialization;

    /// <summary>
    /// The replacement values options.
    /// </summary>
    public enum ReplacementValueOption
    {
        /// <summary>
        /// Use the specfied value within the rule.
        /// </summary>
        Specified = 0,

        /// <summary>
        /// Use the specified value as a calculation expression.
        /// </summary>
        Expression = 1
    }

    /// <summary>
    /// Defines the Replacement type.
    /// </summary>
    public class Replacement
    {
        /// <summary>
        /// Gets or sets the target field.
        /// </summary>
        /// <value>The target field.</value>
        [XmlAttribute(AttributeName = "targetfield")]
        public string TargetField
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating the ReplacementOption.
        /// </summary>
        /// <value>
        /// The type of value replacment.
        /// </value>
        [XmlAttribute(AttributeName = "type")]
        public ReplacementValueOption ReplacementOption
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating the decimal place depth.
        /// </summary>
        /// <value>
        /// The number of decimal places to round the result to.
        /// </value>
        [XmlIgnore]
        public int? DecimalPlaces
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating the decimal place depth.
        /// </summary>
        /// <value>
        /// The number of decimal places to round the result to.
        /// </value>
        [XmlAttribute(AttributeName = "decimalplaces")]
        public string XmlDecimalPlaces
        {
            get
            {
                return this.DecimalPlaces.HasValue ? this.DecimalPlaces.ToString() : null;
            }

            set
            {
                int intValue;

                this.DecimalPlaces = 
                    string.IsNullOrEmpty(value) || !int.TryParse(value, out intValue)
                             ? null
                             : (int?)intValue;
            }
        }

        /// <summary>
        /// Gets or sets replacment value or expression.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        [XmlText]
        public string Value
        {
            get;
            set;
        }
    }
}
